<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <!-- 导入页面的基本样式 -->
  <link rel="stylesheet" href="./css/search.css" />
  <!-- 导入 jQuery -->
  <script src="./lib/jquery.js"></script>
  <!-- 导入模板引擎 -->
  <script src="./lib/template-web.js"></script>
</head>

<body>
  <div class="container">
    <!-- Logo -->
    <img src="./images/taobao_logo.png" alt="" class="logo" />

    <div class="box">
      <!-- tab 栏 -->
      <div class="tabs">
        <div class="tab-active">宝贝</div>
        <div>店铺</div>
      </div>
      <!-- 搜索区域（搜索框和搜索按钮） -->
      <div class="search-box">
        <input id="ipt" type="text" class="ipt" placeholder="请输入要搜索的内容" /><button class="btnSearch">
          搜索
        </button>
      </div>
      <!-- 搜索建议列表 -->
      <div id="suggest-list"></div>
    </div>
  </div>
  <script>
    $(() => {
      //请求函数
      const getSuggestList = (content) => {
        $.ajax({
          dataType: 'jsonp',
          url: 'https://suggest.taobao.com/sug?q=' + content,
          success: res => {
            $('#suggest-list').html('')
            $('#suggest-list').append(res.result.map(item => `<div class="suggest-item">${item[0]}</div>`).join(''))
          }
        })
      }

      // 为输入框绑定 keyup 事件
      $('#ipt').on('keyup', () => {
        const content = $('#ipt').val().trim()
        //判断无内容就退出
        if (content === '') {
          return $('#suggest-list').val('').hide()
        } else {
          $('#suggest-list').show()
        }
        getSuggestList(content)
      })
    })

  </script>

</body>

</html>